/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#ifdef __FCCLIENTSOCKET_H__
#else
#define __FCCLIENTSOCKET_H__

#include "..\\..\\stdafx.h"
#pragma comment(lib, "ws2_32.lib")
#include "winsock2.h"
#include <ws2tcpip.h>
#include <time.h>
#include <string>
#include <vector>
#include "CBase64.h"
#include "FCClientSockets.h"

namespace FaceCat{
    enum ConnectStatus{
        SUCCESS,
        CONNECT_PROXY_FAIL,
        NOT_CONNECT_PROXY,
        CONNECT_SERVER_FAIL
    };

    struct TSock4req1{
        char VN;
        char CD;
        unsigned short Port;
        unsigned long IPAddr;
        char other;
    };

    struct TSock4ans1{
        char VN;
        char CD;
    };

    struct TSock5req1{
        char Ver;
        char nMethods;
        char Methods;
    };

    struct TSock5ans1{
        char Ver;
        char Method;
    };

    struct TSock5req2{
        char Ver;
        char Cmd;
        char Rsv;
        char Atyp;
        char other;
    };

    struct TSock5ans2{
        char Ver;
        char Rep;
        char Rsv;
        char Atyp;
        char other;
    };

    struct TAuthreq{
        char Ver;
        char Ulen;
        char Name;
        char PLen;
        char Pass;
    };

    struct TAuthans{
        char Ver;
        char Status;
    };

    /*
    * 客户端套接字连接
    */
    class FCClientSocket{
        bool m_blnProxyServerOk;
        struct addrinfo hints;
        struct addrinfo *res;
        struct addrinfo *res0;
        lstring m_ip;
        u_short m_port;
        lstring m_proxyDomain;
        int m_proxyType;
        lstring m_proxyIp;
        u_short m_proxyPort;
        lstring m_proxyUserName;
        lstring m_proxyUserPwd;
        int m_timeout;
        char m_token[4];
    private:
        /*
        * 标准连接
        */
        ConnectStatus connectStandard();
        /*
        * Http连接
        */
        ConnectStatus connectByHttp();
        /*
        * Sock4连接
        */
        ConnectStatus connectBySock4();
        /*
        * Sock5连接
        */
        ConnectStatus connectBySock5();
        /*
        * 创建socket
        */
        void createSocket();
    public:
        SOCKET m_hSocket;
        RecvMsg m_recvEvent;
        WriteClientLog m_writeLogEvent;
    public:
        /*
        * 创建客户端socket
        */
        FCClientSocket(int proxyType, const lstring &ip, u_short port, const lstring &proxyIp, u_short proxyPort, const lstring &proxyUserName, const lstring &proxyUserPwd, const lstring &proxyDomain, int timeout, char *token);
        /*
        * 销毁对象
        */
        virtual ~FCClientSocket();
    public:
        /*
        * 关闭
        */
        int close(int socketID);
        /*
        * 连接
        */
        ConnectStatus connect();
        /*
        * 代理连接
        */
        ConnectStatus connectProxyServer();
        /*
        * 发送消息
        */
        static int send(int socketID, const char *str, int len);
        /*
        * 打印日志
        */
        void writeLog(int socketID, int localSID, int state, const char *log);
    };
}

#endif